package com.cpit.platform.dao;

import com.cpit.platform.dto.Organization;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface OrganizationRepository extends JpaRepository<Organization, Long> {
	@SuppressWarnings("unchecked")
	Organization save(Organization org);
	void delete(Organization org);
	
	Organization findById(Integer id);
	List<Organization> findByParentId(Integer pid);

	List<Organization> findByIdIn(List<Integer> idList);

	 /**
     * 用于区域管理员用户查询数据
     */
    List<Organization> findByCodeStartingWith(String orgCode);
    Page<Organization> findByNameContainingAndCodeStartingWith(
    		String organizationname, 
    		String organizationCode, 
    		Pageable pageable);

    @Query(value = "select * from Organization o where o.code like :organizationCode% or :organizationCode like concat(o.code, '%')", nativeQuery = true)
	List<Organization> findByNameAndCodeMatchForTree(
			@Param("organizationCode") String organizationCode
			 );

	@Query(value = "select * from Organization o where :organizationCode like concat(o.code, '%')", nativeQuery = true)
	List<Organization> findByNameAndCodeMatchForTreeCommon(
			@Param("organizationCode") String organizationCode
			 );

    /**
     * 用于普通用户查询数据
     */
    List<Organization> findByCode(String orgCode);
    Page<Organization> findByNameContainingAndCode(
    		String organizationname, 
    		String organizationCode, 
    		Pageable pageable);
   
    /**
     * 用于系统功能管理员查询数据
     */
    Page<Organization> findByNameContaining(
    		String organizationname, 
    		Pageable pageable);
}
